using System.Globalization;
using System.Linq;

namespace OpenWaves.HtmlEncoding
{
    public class FormattingToken : HtmlToken
    {
        private readonly string tag;

        public FormattingToken(int startIndex, int endIndex, string tag)
            : base(startIndex, endIndex)
        {
            this.tag = tag;
        }

        protected override void AddMissingTokens(string html)
        {
            AddMissingTokens(html, StartIndex + this.tag.Length + 2, EndIndex - this.tag.Length - 3);            
        }

        public override void Render(IHtmlWriter writer)
        {
            writer.WriteTag(string.Format(CultureInfo.InvariantCulture, "<{0}>", this.tag));

            foreach (var token in Children)
            {
                token.Render(writer);
            }
            writer.WriteTag(string.Format(CultureInfo.InvariantCulture, "</{0}>", this.tag));
        }
    }
}